package br.unifor.mia.sd.proxy;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ProxyUtils {

	public static byte[] readBytesFromClient(Socket clientSocket)
			throws IOException {
		InputStream in = clientSocket.getInputStream();
		ByteArrayOutputStream out = new ByteArrayOutputStream();

		while (in.available()> 0) {
			out.write(in.read());
		}
		return out.toByteArray();
		
	}

	// private static void writeInto(ByteBuffer buffer, Vector<byte[]> current,
	// int length) {
	// int added = 0;
	//		
	// for(int i = 0; i < current.size(); i++) {
	// byte[] myArray = current.get(i);
	// int off = (length - added) > myArray.length ? myArray.length: (length -
	// added);
	// buffer.put(current.get(i), 0, off);
	// }
	//		
	// int pos = byteBuffer.position();
	// int newCapacity = (pos + bytesRead) < capacity ? capacity : (pos +
	// bytesRead);
	// ByteBuffer destiny = ByteBuffer.allocate(newCapacity);
	// destiny.put(byteBuffer.array(), 0, byteBuffer.position());
	// destiny.put(buffer, 0, bytesRead);
	// return destiny;
	// }

	public static void sendBytesToSocket(byte[] msg, Socket serverSocket)
			throws IOException {
		OutputStream out = serverSocket.getOutputStream();
		out.write(msg);
	}
}
